﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BusinessLogic;
using System.Globalization;
using BusinessObjects;

namespace Presentation.Children {
  /// <summary>
  /// Children lookup list form
  /// </summary>
  public partial class ChildrenListForm : Form {

    private ChildrenBll bll = new ChildrenBll( );
    private BusinessObjects.Children children = new BusinessObjects.Children( );
    private BusinessObjects.Child selectedChild = new BusinessObjects.Child( );
    private BusinessObjects.Children selectedChildren = new BusinessObjects.Children( );
    private int selectedPosition;
    private bool allChildren;

    #region - Properties -

    /// <summary>
    /// Gets a value indicating whether all children were available.
    /// </summary>
    /// <value>
    ///   <c>true</c> if all children were available; otherwise, <c>false</c>.
    /// </value>
    public bool AllChildren {
      get {
        return allChildren;
      }
    }

    /// <summary>
    /// Gets or sets the selected child.
    /// </summary>
    /// <value>
    /// The selected child.
    /// </value>
    public BusinessObjects.Child SelectedChild {
      get { return selectedChild; }
      set { selectedChild = value; }
    }

    /// <summary>
    /// Gets or sets the selected position.
    /// </summary>
    /// <value>
    /// The selected position.
    /// </value>
    public int SelectedPosition {
      get { return selectedPosition; }
      set { selectedPosition = value; }
    }

    /// <summary>
    /// Gets or sets the children.
    /// </summary>
    /// <value>
    /// The children.
    /// </value>
    public BusinessObjects.Children Children {
      get { return children; }
      set { children = value; }
    }

    /// <summary>
    /// Gets or sets the selected children.
    /// </summary>
    /// <value>
    /// The selected children.
    /// </value>
    public BusinessObjects.Children SelectedChildren {
      get { return selectedChildren; }
      set { selectedChildren = value; }
    }

    #endregion

    #region - Constructor -

    /// <summary>
    /// Initializes a new instance of the <see cref="ChildrenListForm"/> class.
    /// </summary>
    public ChildrenListForm( ) {
      InitializeComponent( );
      childrenGridView.AutoGenerateColumns = false;
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="ChildrenListForm"/> class.
    /// </summary>
    /// <param name="multiSelect">if set to <c>true</c> [multi select].</param>
    public ChildrenListForm( bool multiSelect )
      : this( ) {
      childrenGridView.MultiSelect = multiSelect;
    }

    #endregion

    #region - Events -

    private void ChildrenListForm_Load( object sender, EventArgs e ) {
      allChildren = allChildrenRadioButton.Checked;
      children = bll.GetChildren( allChildrenRadioButton.Checked );
      childrenBindingSource.DataSource = children;
      if ( children.Count > 0 ) {
        childrenGridView.Rows[ 0 ].Selected = true;
      }

    }

    private void allChildrenRadioButton_CheckedChanged( object sender, EventArgs e ) {
      allChildren = allChildrenRadioButton.Checked;
      children = bll.GetChildren( allChildrenRadioButton.Checked );
      childrenBindingSource.DataSource = children;
      childrenBindingSource.ResetBindings( false );
    }

    private void childrenGridView_DataBindingComplete( object sender, DataGridViewBindingCompleteEventArgs e ) {
      DataGridViewCell valueCell = null;
      DataGridViewImageCell displayCell = null;
      for ( int i = 0; i < childrenGridView.Rows.Count; i++ ) {
        valueCell = childrenGridView[ ChildActiveState.Index, i ];
        displayCell = ( DataGridViewImageCell ) childrenGridView[ displayState.Index, i ];
        displayCell.Value = stateImageList.Images[ Convert.ToInt32( valueCell.Value, CultureInfo.CurrentCulture ) ];
      }

    }

    private void childrenGridView_CellDoubleClick( object sender, DataGridViewCellEventArgs e ) {
      okButton.PerformClick( );
    }

    private void okButton_Click( object sender, EventArgs e ) {
      if ( childrenGridView.SelectedRows.Count > 0 ) {
        selectedPosition = childrenGridView.SelectedRows[ 0 ].Index;
        selectedChild = ( BusinessObjects.Child ) childrenGridView.SelectedRows[ 0 ].DataBoundItem;
      }
    }

    private void ChildrenListForm_FormClosing( object sender, FormClosingEventArgs e ) {
      if ( childrenGridView.SelectedRows.Count > 0 ) {
        foreach ( DataGridViewRow row in childrenGridView.SelectedRows ) {
          SelectedChildren.Add( ( Child ) row.DataBoundItem );
        }
      }
    }

    #endregion


  }
}
